package bidsanity



import grails.test.mixin.*
import org.junit.*

/**
 * See the API for {@link grails.test.mixin.web.ControllerUnitTestMixin} for usage instructions
 */
@TestFor(ListingController)
@Mock([Customer, Bid, Listing, CustomerService])
class ListingControllerTests {
    def customerService = new CustomerService()

    void testIndex() {
        def controllerResults = controller.index()
        Assert.assertNotNull(controllerResults.listingInstanceList)
        Assert.assertNotNull(controllerResults.listingInstanceTotal)
    }

    void testSave() {
        controller.listingService = new ListingService()
        Assert.assertNotNull(customerService)
        def s = customerService.createCustomer(emailAddress: 'seller@example.org', password: "testing")
        Assert.assertNotNull(s.id)
        controller.session.user = s.emailAddress
        Map myParams = [
                name: 'TestListing',
                endDate: new Date() + 10,
                startingBidPrice: 1.0D,
                description: 'a test listing']
        controller.params.putAll(myParams)
        controller.save()
        Assert.assertEquals(Listing.count(), 1)
        Assert.assertNotNull(Listing.list()[0].id)
        Assert.assertTrue(response.redirectedUrl == "/listing/show/${Listing.list()[0].id}")
    }

    void testSaveInvalid() {
        controller.listingService = new ListingService()
        Assert.assertNotNull(customerService)
        def s = customerService.createCustomer(emailAddress: 'seller@example.org', password: "testing")
        Assert.assertNotNull(s.id)
        controller.session.user = s.emailAddress
        Map myParams = [
                name: 'TestListing',
                endDate: new Date() - 10,
                startingBidPrice: null,
                description: 'a test listing',
                sellerId: s.id]
        controller.params.putAll(myParams)
        controller.save()
        Assert.assertEquals(Listing.count(), 0)
        Assert.assertTrue(view == "/listing/create")
        Assert.assertNotNull(model.listingInstance)
        Assert.assertEquals(flash.message, "default.not.created.message")
    }
}
